package org.qiyu.live.msg.provider.consumer.handler.Impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.apache.dubbo.config.annotation.DubboReference;
import org.qiyu.im.router.interfaces.rpc.ImRouterRpc;
import org.qiyu.live.im.constants.AppIdEnum;
import org.qiyu.live.im.dto.ImMsgBody;
import org.qiyu.live.msg.dto.MessageDTO;
import org.qiyu.live.msg.enums.ImMsgBizCodeEnum;
import org.qiyu.live.msg.provider.consumer.handler.MessageHandler;
import org.springframework.stereotype.Component;

/**
 * @Description: TODO
 * @Author zhangguangyan
 * @Date 2023/10/01/ 12:09:38
 */
@Component
public class SingleMessageHandlerImpl implements MessageHandler {
    @DubboReference
    private ImRouterRpc imRouterRpc;

    @Override
    public void onMsgReceiver(ImMsgBody imMsgBody) {
        int bizCode = imMsgBody.getBizCode();
        //直播间的消息
        if (bizCode == ImMsgBizCodeEnum.LIVING_ROOM_IM_CHAT_BIZ.getCode()) {
            MessageDTO messageDTO = JSON.parseObject(imMsgBody.getData(), MessageDTO.class);
            //todo 暂时不做处理
            ImMsgBody resoMsg = new ImMsgBody();
                resoMsg.setUserId(messageDTO.getUserId());
                resoMsg.setAppId(AppIdEnum.QIYU_LIVE_BIZ.getCode());
                resoMsg.setBizCode(ImMsgBizCodeEnum.LIVING_ROOM_IM_CHAT_BIZ.getCode());
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("senderId",messageDTO.getUserId());
            jsonObject.put("content",messageDTO.getContent());
            resoMsg.setData(jsonObject.toJSONString());
            imRouterRpc.sendMsg(resoMsg);
        }
    }
}
